package com.xrh.core.bean;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.CollUtil;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * @author 肖荣辉
 * @function 包含集合和总条数以及分页参数的封装对象
 * @date 2020年11月7日
 */
@Data
public class CommonPage<T> implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 当前页码
     */
    private Integer pageNum;

    /**
     * 每页数量
     */
    private Integer pageSize;

    /**
     * 总页数
     */
    private Long totalPage;

    /**
     * 总条数
     */
    private Long total;

    /**
     * 分页数据
     */
    private List<T> list;

    public static <T> CommonPage<T> createEmpty(int pageNum, int pageSize) {
        return new CommonPage<T>(pageNum, pageSize, getTotalPage(pageNum, pageSize, 0L), 0L, new ArrayList<>());
    }

    public static <T> CommonPage<T> create(List<T> list, long total) {
        return new CommonPage<T>(null, null, null, total, list);
    }

    public static <T> CommonPage<T> create(BasePage basePage, List<T> list, long total) {
        long totalPage = getTotalPage(basePage.getPageNum(), basePage.getPageSize(), total);
        return new CommonPage<T>(basePage.getPageNum(), basePage.getPageSize(), totalPage, total, list);
    }

    public static <T> CommonPage<T> create(int pageNum, int pageSize, List<T> list, long total) {
        return new CommonPage<T>(pageNum, pageSize, getTotalPage(pageNum, pageSize, total), total, list);
    }

    public static long getTotalPage(int pageNum, int pageSize, long total) {
        return (total - 1) / pageSize + 1;
    }

    private CommonPage(Integer pageNum, Integer pageSize, Long totalPage, Long total, List<T> list) {
        super();
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.totalPage = totalPage;
        this.total = total;
        this.list = list;
    }

    /**
     * 创建内存分页
     *
     * @param list     内存中集合
     * @param pageNum  第几页
     * @param pageSize 每页数量
     * @return 集合分页
     */
    public static <T> CommonPage<T> createMemoryPage(List<T> list, Integer pageNum, Integer pageSize) {
        return createMemoryPage(list, pageNum, pageSize, Function.identity());
    }

    /***
     * @description: 创建内存分页
     * @author: huangyufa
     * @date: 2023/9/14 16:43
     * @param: list  内存中集合
     * @param: pageNum 第几页
     * @param: pageSize 每页数量
     * @param: function collection中的泛型转化为list泛型的lambda表达式
     * @param <T>        collection中的泛型
     * @param <E>        List中的泛型
     * @return: com.xrh.core.bean.CommonPage<T>
     */
    public static <T, E> CommonPage<E> createMemoryPage(List<T> list, Integer pageNum, Integer pageSize, Function<T, E> function) {
        if (CollUtil.isEmpty(list)) {
            return createEmpty(pageNum, pageSize);
        }
        long totalPage = getTotalPage(pageNum, pageSize, list.size());
        int start = (pageNum - 1) * pageSize;
        List<T> sub = CollUtil.sub(list, start, start + pageSize);
        return new CommonPage<E>(pageNum, pageSize, totalPage, (long) list.size(), CollStreamUtil.toList(sub, function));
    }

    public CommonPage() {
        super();
    }

}
